package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.netty.util.concurrent.CompleteFuture;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {


    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;
    @Autowired
    private CategoryFeignClient categoryFeignClient;
    @Autowired
    private UserInfoFeignClient userInfoFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private RedissonClient redissonClient;

    @Override
    public Map<String, Object> getItem(Long albumId) {
        Map<String, Object> map = new HashMap<>();
//        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
//        if (!bloomFilter.contains(albumId)){
//            log.error("专辑Id: {} 不在布隆过滤器中", albumId);
//            return map;
//        }
        //专辑信息
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            Result<AlbumInfo> result = albumInfoFeignClient.getAlbumInfo(albumId);
            Assert.notNull(result, "查询专辑信息失败");
            AlbumInfo albumInfo = result.getData();
            Assert.notNull(albumInfo, "查询专辑信息失败");
            map.put("albumInfo", albumInfo);
            return albumInfo;
        }, threadPoolExecutor).whenCompleteAsync((albumInfo, throwable)->{
            System.out.println("专辑标题"+albumInfo.getAlbumTitle());
            if (throwable != null){
                log.error("查询专辑信息失败: {}", throwable.getMessage());
            }
        });
        //分类信息
        CompletableFuture<Void> cateCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            Result<BaseCategoryView> result = categoryFeignClient.getCategoryView(albumInfo.getCategory3Id());
            Assert.notNull(result, "查询分类对象失败");
            BaseCategoryView baseCategoryView = result.getData();
            Assert.notNull(baseCategoryView, "查询分类对象失败");
            map.put("baseCategoryView", baseCategoryView);
        }, threadPoolExecutor).whenCompleteAsync((baseCategoryView, throwable) -> {
            if (throwable != null){
                log.error("查询分类对象失败: {}", throwable.getMessage());
            }
        });
        //作者信息
        CompletableFuture<Void> userCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            Result<UserInfoVo> userInfoVo = userInfoFeignClient.getUserInfoVo(albumInfo.getUserId());
            Assert.notNull(userInfoVo, "查询作者信息失败");
            UserInfoVo data = userInfoVo.getData();
            Assert.notNull(data, "查询作者信息失败");
            map.put("announcer", data);
        }, threadPoolExecutor).whenCompleteAsync((userInfoVo, throwable) -> {
            if (throwable != null){
                log.error("查询作者信息失败: {}", throwable.getMessage());
            }
        });
        //  根据专辑Id 查询专辑统计数据;
        CompletableFuture<Void> statCompletableFuture = CompletableFuture.runAsync(() -> {
            //  远程调用;
            Result<AlbumStatVo> albumStatVoResult = albumInfoFeignClient.getAlbumStatVo(albumId);
            Assert.notNull(albumStatVoResult, "查询专辑统计数据失败");
            AlbumStatVo albumStatVo = albumStatVoResult.getData();
            Assert.notNull(albumStatVo, "查询专辑统计数据失败");
            map.put("albumStatVo", albumStatVo);
        }, threadPoolExecutor).whenCompleteAsync((albumStatVo, throwable)-> {
            if ( throwable!= null) {
                log.error("查询统计数据失败：{}", throwable.getMessage());
            }
        });
        try {
            CompletableFuture.allOf(albumInfoCompletableFuture, cateCompletableFuture, userCompletableFuture, statCompletableFuture).join();
        } catch (Exception e) {
            throw new GuiguException(20001, "远程调用获取数据失败");
        }
        return map;
    }
}
